package com.lxl.bt.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lxl.bt.Constant;
import com.lxl.bt.R;
import com.lxl.bt.util.ACache;
import com.lxl.bt.util.BitMapUtils;
import com.lxl.bt.util.Prefs;
import com.yanzhenjie.sofia.Sofia;
import com.yzq.zxinglibrary.encode.CodeCreator;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by lvxinlong
 * on  邀请好友
 * on  2021年4月8日22:37:20
 **/
public class ShareActivity extends BaseActivity {


    @BindView(R.id.ivQr)
    ImageView ivQr;
    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.rlRight)
    RelativeLayout rlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rl_poster)
    RelativeLayout rlPoster;

    private Context context;
    ACache aCache;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        ButterKnife.bind(this);
        //状态栏全透明全屏显示页面，布局能够延伸到状态栏和导航栏下方
        Sofia.with(this)
                .invasionStatusBar()
                .statusBarLightFont()
                .statusBarBackgroundAlpha(0)
                .invasionNavigationBar()
                .navigationBarBackgroundAlpha(0);
        context = this;
        rlBack.setVisibility(View.VISIBLE);
        txtToolbarTitle.setText("邀请好友");
        rlRight.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("保存图片");

        aCache = ACache.get(this);
        Bitmap qrCode = CodeCreator.createQRCode("http://reg.puxkj.com/?visCode=" + Prefs.getStringValue(Constant.VISTORCODE), 400, 400, null);
        ivQr.setImageBitmap(qrCode);

        tvRight.setOnClickListener(v -> {
            savePoster();
        });
    }

    private void savePoster() {
        // 1.View截图
        rlPoster.setDrawingCacheEnabled(true);
        // 重新测量View
        rlPoster.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        // 2.创建一个Bitmap
        Bitmap mBitmap = rlPoster.getDrawingCache();
        // 3.保存到SD卡
        BitMapUtils.saveBmp2Gallery(mBitmap, "邀请码-"+Prefs.getStringValue(Constant.VISTORCODE), context);
    }

    @OnClick(R.id.rlBack)
    public void onClick() {
        finish();
    }
}
